<template>
  <div>
    <el-dialog
      :title="ruleForm.id == '' ? '添加员工信息' : '编辑员工信息'"
      :visible.sync="addEditIsDialog_c"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="120px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司名称：" prop="companyId">
              <el-cascader
                v-model="ruleForm.companyId"
                placeholder="请选择公司名称"
                :options="companyArray"
                :props="{
                  checkStrictly: true,
                  value: 'id',
                  label: 'name',
                  children: 'childList',
                }"
                @change="changeTreeItem"
                clearable
                :disabled="seeType"
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="部门名称：" prop="departId">
              <el-cascader
                v-model="ruleForm.departId"
                placeholder="请选择部门名称"
                :options="departArray"
                :props="{
                  checkStrictly: true,
                  value: 'id',
                  label: 'name',
                  children: 'childList',
                }"
                @change="changeTreeItemDepart"
                clearable
                :disabled="seeType"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职位名称：" prop="departPositionId">
              <el-select
                v-model="ruleForm.departPositionId"
                placeholder="请选择职位名称"
                clearable
                :disabled="seeType"
              >
                <el-option
                  v-for="item in departPositionArray"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工号：" prop="code">
              <el-input
                v-model="ruleForm.code"
                placeholder="请输入工号"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名：" prop="realName">
              <el-input
                v-model="ruleForm.realName"
                placeholder="请输入姓名"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="头像：" prop="headImage">
              <UploadImg
                para="headImage"
                @setImg="setImg"
                :imgUrl="ruleForm.headImage"
                width="100"
                height="100"
              ></UploadImg>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别：" prop="sex">
              <el-select
                v-model="ruleForm.sex"
                placeholder="请选择性别"
                clearable
                :disabled="seeType"
              >
                <el-option label="男" :value="0"></el-option>
                <el-option label="女" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号：" prop="idCard">
              <el-input
                v-model="ruleForm.idCard"
                placeholder="请输入身份证号"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期：" prop="birthday">
              <el-date-picker
                v-model="ruleForm.birthday"
                placeholder="请选择出生日期"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                clearable
                :disabled="seeType"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码：" prop="phone">
              <el-input
                v-model="ruleForm.phone"
                placeholder="请输入手机号码"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱地址：" prop="email">
              <el-input
                v-model="ruleForm.email"
                placeholder="请输入邮箱地址"
                clearable
                maxlength="100"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import UploadImg from "@/components/UploadImg.vue";

import {
  getEmployeInfo,
  saveEmployeInfo,
  getCompanyInfoTree,
  getDepartInfoTree,
  getDepartPositions,
} from "@/api/company";

export default {
  components: {
    UploadImg,
  },
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        companyId: "",
        departId: "",
        departPositionId: "",
        code: "",
        realName: "",
        headImage: "",
        sex: "",
        idCard: "",
        birthday: "",
        phone: "",
        email: "",
      },
      rules: {
        // 表单规则
        companyId: [
          { required: true, message: "请选择公司名称", trigger: "change" },
        ],
        departId: [
          { required: true, message: "请选择部门名称", trigger: "change" },
        ],
        departPositionId: [
          { required: true, message: "请选择职位名称", trigger: "change" },
        ],
        code: [{ required: true, message: "请输入工号", trigger: "blur" }],
        realName: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        headImage: [{ required: true, message: "请上传头像", trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        idCard: [
          { required: true, message: "请输入身份证号", trigger: "blur" },
        ],
        birthday: [
          { required: true, message: "请选择出生日期", trigger: "blur" },
        ],
        phone: [
          { required: false, message: "请输入手机号码", trigger: "blur" },
        ],
        email: [
          { required: false, message: "请输入邮箱地址", trigger: "blur" },
        ],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      companyArray: [], // 公司集合
      departArray: [], // 部门集合
      departPositionArray: [], // 职位集合
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        // 获取公司信息树形列表
        this.getCompanyInfoTree();
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getEmployeInfo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;

          // 获取部门信息树形列表
          this.getDepartInfoTree();

          // 获取职位信息列表
          this.getDepartPositions();
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveEmployeInfo(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 获取公司信息树形列表
    getCompanyInfoTree() {
      getCompanyInfoTree().then((res) => {
        if (res.code == 200) {
          this.companyArray = res.data;
        }
      });
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.ruleForm.companyId = val[val.length - 1];
      } else {
        this.ruleForm.companyId = "";
      }

      // 清空
      this.ruleForm.departId = "";
      this.departArray = [];

      this.ruleForm.departPositionId = "";
      this.departPositionArray = [];

      // 获取部门信息树形列表
      this.getDepartInfoTree();
    },
    // 获取部门信息树形列表
    getDepartInfoTree() {
      if (!this.ruleForm.companyId || this.ruleForm.companyId == "") {
        return;
      }
      getDepartInfoTree({ companyId: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.departArray = res.data;
        }
      });
    },
    // 部门下拉切换
    changeTreeItemDepart(val) {
      if (val) {
        this.ruleForm.departId = val[val.length - 1];
      } else {
        this.ruleForm.departId = "";
      }

      // 清空
      this.ruleForm.departPositionId = "";
      this.departPositionArray = [];

      // 获取职位信息列表
      this.getDepartPositions();
    },
    // 获取职位信息列表
    getDepartPositions() {
      if (!this.ruleForm.departId || this.ruleForm.departId == "") {
        return;
      }
      getDepartPositions({ id: this.ruleForm.departId }).then((res) => {
        if (res.code == 200) {
          this.departPositionArray = res.data;
        }
      });
    },
    // 上传图片
    setImg(url) {
      this.ruleForm.headImage = url;
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-cascader {
    width: 100%;
  }
}
</style>
